const ResultCode = require('./ResultCode');

/**
 * @author 长矛
 * @description 统一返回结果
 */
class Result {
   
    /**
     * 返回code
     */
    code;
    /**
     * 返回消息
     */
    message;
    /**
     * 返回数据
     */
    data;
    /**
     * 是否成功
     */
    success;
    /**
     * 返回时间
     */
    time;

    /**
     * 
     * @param code {number} 返回code
     * @param msg {string} 返回消息
     * @param data {any} 返回具体对象
     * @param {boolean} success 本次请求状态
     */
    constructor(code, msg, data,success) {
        this.code = code;
        this.message = msg;
        this.data = data;
        this.success=success;
        this.time = Date.now();
    }

    /**
     * 成功
     * @param data {any} 返回对象
     * @return {Result}
     */
    static success(data) {
        return new Result(ResultCode.SUCCESS.code, ResultCode.SUCCESS.desc, data,true);
    }

    /**
     * 参数校验失败
     */
    static validateFailed(param) {
        return new Result(ResultCode.VALIDATE_FAILED.code, ResultCode.VALIDATE_FAILED.desc, param,false);
    }
    /**
     * 由于未携带token或token过期导致请求未通过身份验证
     * @return {Result}
     */
    static unauthenticated(){
        return new Result(ResultCode.UNAUTHENTICATED.code,ResultCode.UNAUTHENTICATED.desc,null,false)
    }
    /**
     * 携带的token没有足够权限
     */
    static permissionDenied(){
        return new Result(ResultCode.PERMISSION_DENIED.code,ResultCode.PERMISSION_DENIED.desc,null,false)
    }
    /**
     * Api接口不存在
     * 
     */
    static apiNotFount(){
        return new Result(ResultCode.API_NOT_FOUNT.code,ResultCode.API_NOT_FOUNT.desc,null,false)
    }
    /**
     * 未知错误
     */
    static unknownError(error){
        return new Result(ResultCode.UNKNOWN_ERTOR.code,ResultCode.UNKNOWN_ERTOR.desc,error,false)
    }
}

module.exports=Result
